home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 13990 / 13990.xpi / chrome / clickYoutubeDownloader.jar / content / clickYoutube_overlay.js next >
Text File  |  2010-01-14  |  9KB  |  233 lines

  1. var clickYoutubeD = {
  2.     onLoad : function() {
  3.         clickYoutubeD.loadUserPrefs();
  4.         clickYoutubeD.checkVer();
  5.         window.document.addEventListener("DOMContentLoaded", clickYoutubeD.DOMContentLoaded, true)
  6.     },
  7.     loadUserPrefs : function() {
  8.         var Application = Components.classes["@mozilla.org/fuel/application;1"].getService(Components.interfaces.fuelIApplication);
  9.         var Extension = Application.extensions.get("YoutubeDownloader@PeterOlayev.com");
  10.         clickYoutubeD.prefs = Extension.prefs;
  11.  
  12.         clickYoutubeD.userOptions = {
  13.             flv : true,
  14.             mp4 : true,
  15.             _3gp : true,
  16.             hd  : true,
  17.             fullHD : true
  18.         }
  19.     },
  20.     checkVer : function() {
  21.         try {
  22.               var nsIExtensionManager = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
  23.               var addonVersion          = nsIExtensionManager.getItemForID("YoutubeDownloader@PeterOlayev.com").version;
  24.               var lastCheckVersion    = clickYoutubeD.prefs.getValue("addonVersion", "");
  25.               var newU                = clickYoutubeD.prefs.getValue("new", true);
  26.               if (newU) {
  27.                   clickYoutubeD.prefs.setValue("addonVersion", addonVersion);
  28.                   clickYoutubeD.prefs.setValue("new", false);
  29.                   window.setTimeout(function(){
  30.                       var b = getBrowser();
  31.                       b.selectedTab = b.addTab("http://www.clickyoutubedownload.com/youtube/?t=d&v=" + addonVersion);
  32.                   }, 1200);
  33.               }    else {
  34.                   if (lastCheckVersion!=addonVersion){
  35.                         clickYoutubeD.prefs.setValue("addonVersion", addonVersion);
  36.                         window.setTimeout(function(){
  37.                           var b = getBrowser();
  38.                           b.selectedTab = b.addTab("http://www.clickyoutubedownload.com/youtube/?t=u&v=" + addonVersion);
  39.                         }, 1200);
  40.                   }                
  41.               }
  42.               return newU;
  43.         } catch(ex) {}
  44.     },
  45.     getQueryString : function (href, ji) {
  46.         gy = href.split("&");
  47.         for (i=0;i<gy.length;i++) {
  48.             ft = gy[i].split("=");
  49.             if (ft[0] == ji) {
  50.                 return ft[1];
  51.             }
  52.         }
  53.     },
  54.     getQuery : function(doc, param) {
  55.         var query = clickYoutubeD.getQueryString(doc.location.search.substring(1), param);    
  56.  
  57.         if ((query=="") || (query==null)) {
  58.             query = clickYoutubeD.getQueryString(doc.location.hash, param);    
  59.         }    
  60.         return query;
  61.     },
  62.     DOMContentLoaded : function(event) {
  63.         var doc = event.originalTarget;
  64.         var loc = doc.location;            
  65.         var u = doc.location.toString();
  66.         if (loc.href.match(/http:\/\/www(|[0-9])\.(|l\.)youtube\..*\/.*/i)) {
  67.             doc.videoID = clickYoutubeD.getQuery(doc, "v");
  68.             if (doc.videoID!=null) {
  69.                 doc.videoTitle   = doc.title.replace(/youtube - /gi, "")
  70.                 var downloadLinks = doc.createElement("div");
  71.                 downloadLinks.style.cssFloat = "right"
  72.                 downloadLinks.style.paddingRight="22px;"
  73.                 var watchViewsDiv    = doc.getElementById("watch-views-div");;
  74.                 watchViewsDiv.style.width = "auto";
  75.                 watchViewsDiv.parentNode.insertBefore(downloadLinks, watchViewsDiv)
  76.  
  77.                 if (clickYoutubeD.userOptions.flv) downloadLinks.appendChild(clickYoutubeD.setLink(doc, "FLV" ,"FLV"));
  78.                 if (clickYoutubeD.userOptions.mp4) downloadLinks.appendChild(clickYoutubeD.setLink(doc, "MP4", "MP4 - 480x360"));
  79.                 if (clickYoutubeD.userOptions._3gp) downloadLinks.appendChild(clickYoutubeD.setLink(doc, "3GP", "3GP - 320x240"));
  80.  
  81.                 if (clickYoutubeD.userOptions.hd) {
  82.                     if (clickYoutubeD.isVideoSupportHD(doc)) {
  83.                         downloadLinks.appendChild(clickYoutubeD.setLink(doc, "HD", "HD 720p - 1280x720"));
  84.                     }
  85.                 }
  86.                 if (clickYoutubeD.userOptions.fullHD) {
  87.                     if (clickYoutubeD.isVideoSupportFullHD(doc)) {
  88.                         downloadLinks.appendChild(clickYoutubeD.setLink(doc, "/ FULL HD", "HD 1080p - 1920x1080"));
  89.                     }
  90.                 }
  91.  
  92.                 var icon = doc.createElement("div");
  93.                 icon.style.backgroundImage = "url(chrome://clickYoutubeD/content/icons/icon_small.png)"
  94.                 icon.style.backgroundRepeat = "no-repeat"
  95.                 icon.style.width = "16px";
  96.                 icon.style.height = "16px";
  97.                 icon.style.cssFloat = "right"
  98.                 icon.style.marginLeft = "4px"
  99.                 downloadLinks.appendChild(icon);
  100.             }
  101.         }
  102.     },
  103.     
  104.     isVideoSupportHD : function(doc) {
  105.         var moviePlayer = doc.getElementById('movie_player');
  106.         if (moviePlayer) {
  107.             var flashvars = moviePlayer.attributes.getNamedItem('flashvars');  
  108.             if (flashvars!=null) {
  109.                 if (flashvars.value.indexOf("fmt_map=22")!=-1) {
  110.                     return true;
  111.                 } else if (flashvars.value.indexOf("22%2F2000000%")!=-1) {
  112.                     return true;
  113.                 }
  114.             }
  115.         } else {
  116.             var innerHTML = doc.getElementsByTagName("html")[0].innerHTML;
  117.             if (innerHTML) {
  118.                 if (innerHTML.indexOf("22%2F2000000%")!=-1) {
  119.                     return true;
  120.                 }
  121.             }
  122.         }
  123.         return false;
  124.     },
  125.     
  126.     isVideoSupportFullHD : function(doc) {
  127.         var moviePlayer = doc.getElementById('movie_player');
  128.         if (moviePlayer) {
  129.             var flashvars = moviePlayer.attributes.getNamedItem('flashvars');  
  130.             if (flashvars!=null) {
  131.                 if (flashvars.value.indexOf("fmt_map=37")!=-1) {
  132.                     return true;
  133.                 }
  134.             }
  135.         } else {
  136.             var innerHTML = doc.getElementsByTagName("html")[0].innerHTML;
  137.             if (innerHTML) {
  138.                 if (innerHTML.indexOf("37%2F4000000")!=-1) {
  139.                     return true;
  140.                 }
  141.             }
  142.         }
  143.         return false;
  144.     },
  145.  
  146.     setLink : function(doc, type, tooltip) {
  147.         var l = doc.createElement("a");
  148.         l.style.marginLeft = "3px"
  149.         l.videoType = type;
  150.         l.videoID  = doc.videoID
  151.         l.doc = doc
  152.         l.innerHTML = type
  153.         l.setAttribute("href", "#")
  154.         l.setAttribute("onclick", "return false")
  155.         if (tooltip) {
  156.             l.setAttribute("title", tooltip)
  157.         }
  158.         l.addEventListener("click",function(e) { 
  159.             var target = e.currentTarget;
  160.             var url  = clickYoutubeD.getYoutubeVideoUrl(target.videoID, 
  161.                                                   target.doc, 
  162.                                                   target.videoType);
  163.             if ((target.videoType.toLowerCase()=="hd") || (target.videoType.toLowerCase()=="/ full hd")) {
  164.                 clickYoutubeD.downloadFile(doc.videoTitle, url, "mp4");            
  165.             } else {
  166.                 clickYoutubeD.downloadFile(doc.videoTitle, url, target.videoType);
  167.             }
  168.         },false);
  169.         return l;
  170.     },
  171.     getYoutubeVideoUrl : function(videoID, doc, videoType) {
  172.         var innerHTML = doc.getElementsByTagName("html")[0].innerHTML;
  173.         var reg = /\&t=[\w-]{10,}/ig;
  174.         var t = innerHTML.match(reg)
  175.  
  176.         var p1 = innerHTML.indexOf("\"t\":") + 6;
  177.         var p2 = innerHTML.indexOf("\",", p1);
  178.         var t  = innerHTML.substring(p1, p2 - 3);
  179.         t = "t=" + t
  180.         var moviePlayer = doc.getElementById('movie_player');
  181.         if (moviePlayer) {
  182.             var flashvars = moviePlayer.attributes.getNamedItem('flashvars');  
  183.             if (flashvars!=null) {
  184.                 var t = flashvars.value.match(reg)
  185.                 if (t.length>0) t = t[0]
  186.             } else return false;
  187.         }
  188.         t += "=";
  189.  
  190.         if (videoType.toLowerCase()=="hd") {
  191.             var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=22"
  192.         } else if (videoType.toLowerCase()=="/ full hd") {
  193.             var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=37"
  194.         } else if (videoType.toLowerCase()=="mp4") {
  195.             var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=18"
  196.         } else if (videoType.toLowerCase()=="3gp") {
  197.             var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=17"
  198.         } else {
  199.             var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t
  200.         }
  201.         return downloadURL;
  202.     },
  203.     getFileToDownload : function(fileName, fileType) {
  204.         var nsIFilePicker = Components.interfaces.nsIFilePicker;
  205.         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  206.         fp.init(window, "Save As", nsIFilePicker.modeSave);
  207.         fp.defaultString = fileName;
  208.         fp.appendFilter(fileType, "*." + fileType);
  209.         var rv = fp.show();
  210.         if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
  211.           var file = fp.file;
  212.           var path = fp.file.path;
  213.           return file;
  214.         }
  215.         return null;
  216.     },
  217.     downloadFile : function(title, url, fileType) {
  218.         if (fileType.toLowerCase()=="hd") title += "_hd";
  219.         var fileName = title +"."+fileType
  220.         var file    = clickYoutubeD.getFileToDownload(fileName, fileType);
  221.         var persist = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Components.interfaces.nsIWebBrowserPersist);  
  222.         var ios     = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);  
  223.         var xfer    = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);  
  224.         
  225.         var uri     = ios.newURI(url, null, null); 
  226.         var target  = ios.newFileURI(file);
  227.         xfer.init(uri, target, "", null, null, null, persist);  
  228.         persist.progressListener = xfer; 
  229.         persist.saveURI(uri, null, null, null, null, file);
  230.     }
  231. }
  232. window.addEventListener("load", clickYoutubeD.onLoad, false);
  233.